package com.yx.frame.builder.xml;

import java.util.ArrayList;
import java.util.List;

public class UpdateMethodContentXml extends MethodContentXml {
	List<UpdateSql> transaction = new ArrayList<UpdateSql>();
	String callback = "";
	String datasource = "";
	String userDefineDatasource = "false";

	public UpdateMethodContentXml() {
		// TODO 自动生成的构造函数存根
	}

	public List<UpdateSql> getTransaction() {
		return transaction;
	}

	public String getCallback() {
		return callback;
	}

	public String getDatasource() {
		return datasource;
	}

	public void setDatasource(String datasource) {
		this.datasource = datasource;
	}

	public void setCallback(String callback) {
		this.callback = callback;
	}

	public void setUserDefineDatasource(String userDefineDatasource) {
		if(null!=userDefineDatasource&&"true".equals(userDefineDatasource)){
			this.userDefineDatasource = "true";
		}else{
			this.userDefineDatasource = "false";
		}
	}

	public String getUserDefineDatasource() {
		return userDefineDatasource;
	}

	public void addUpdateSql(int idx, String sql, String loopby) {
		UpdateSql updateSql = new UpdateSql(idx, sql, loopby);
		getTransaction().add(updateSql);
		sort();
	}

	public void sort() {
		UpdateSql tmpSql;
		for (int i = 1; i < transaction.size(); i++) {
			for (int j = i; (j > 0)
					&& (transaction.get(j).getIndex() < transaction.get(j - 1)
							.getIndex()); j--) {
				tmpSql = transaction.get(j);
				transaction.set(j, transaction.get(j - 1));
				transaction.set(j - 1, tmpSql);
			}
		}
	}

	public StringBuffer toXml() {
		StringBuffer xml = new StringBuffer();
		xml.append("		<Transaction datasource=\"" + getDatasource()+ "\"  userDefineDatasource=\"" + getUserDefineDatasource()+ "\" callback=\"" + getCallback() + "\">\r\n");
		for (int i = 0; i < transaction.size(); i++) {
			UpdateSql sql = transaction.get(i);
			xml.append(sql.toXml());
		}
		xml.append("		</Transaction>\r\n");
		return xml;
	}

	public class UpdateSql {
		int index = 0;
		String sql = "";
		String loopby = "";

		public UpdateSql(int idx, String sql, String loopby) {
			sql = sql.replace("\r", "");
			sql = sql.replace("\n", "");
			sql = sql.replace("\t", " ");
			while (sql.startsWith(" ")) {
				sql = sql.substring(1);
			}
			while (sql.endsWith(" ")) {
				sql = sql.substring(0, sql.length() - 1);
			}
			this.index = idx;
			this.sql = sql;
			this.loopby = loopby;
		}

		public int getIndex() {
			return index;
		}

		public String getSql() {
			return sql;
		}

		public String getLoopby() {
			return loopby;
		}

		public boolean isLoop() {
			if (loopby != null && !"".equals(loopby.trim())) {
				return true;
			} else {
				return false;
			}
		}

		public StringBuffer toXml() {
			StringBuffer xml = new StringBuffer();
			xml.append("			<SQL index=\"" + this.getIndex() + "\" loopby=\""
					+ this.getLoopby() + "\">\r\n");
			xml.append("				<![CDATA[" + this.getSql() + "]]>\r\n");
			xml.append("			</SQL>\r\n");
			return xml;
		}

	}

}
